package com.fast.user.web.mobile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.fast.base.db.page.PageView;
import com.fast.base.model.AnoSysLog;
import com.fast.base.security.StringEncrypt;
import com.fast.base.util.LogUtils;
import com.fast.base.util.web.ResponseData;
import com.fast.base.util.web.ResponseEnum;
import com.fast.base.web.BaseController;
import com.fast.user.model.admin.AdminUser;
import com.fast.user.model.admin.Group;
import com.fast.user.model.dept.Department;
import com.fast.user.service.admin.AdminUserService;
import com.fast.user.service.dept.CompanyService;
import com.fast.user.web.form.web.AdminForm;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 
 *
 * 类名:com.fast.user.web.mobile.AppAdminController <pre>
 * 标题:系统管理员app端接口
 * 编写者:lushuifa
 * 创建时间:2017年12月17日 下午5:18:38
 * </pre>
 */
@Api(value="用户控制类",tags={"用户操作接口"})
@ApiIgnore
@Controller("AppAdminController")
@RequestMapping("/app/admin")
public class AppAdminController extends BaseController{
	
	@Resource(name="adminUserService")
	private AdminUserService adminUserService;
	
	@Resource(name="companyService")
	private CompanyService companyService;
	
	
	/**
	 * 拿到当前登录的信息
	 * @param user 用户对象
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getAdminInfo.do",method = RequestMethod.POST)
	@ApiOperation(value="获取当前管理人员的登录信息", notes="获取当前管理人员的登录信息",httpMethod="POST",consumes="application/x-www-form-urlencoded")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "query", dataType = "String", name = "uuid", value = "用户ID", required = true) })
	public ResponseData getAdminInfo(@RequestParam(value="uuid") String uuid){
		ResponseData responseData = new ResponseData();
		try {
			AdminUser adminUser = this.adminUserService.getAdminUserByUUID(uuid);
			if(null!=adminUser){
				responseData.setData(adminUser);
			}else{
				responseData.failue();
				return responseData;
			}
		}catch (Exception e) {
			LogUtils.info("getAdminInfo.err.登录异常：", e);
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}

	/**
	 * 得到全部[部门]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getAllDeptList.do",method = RequestMethod.POST)
	public ResponseData getAllDeptList() {
		ResponseData responseData = new ResponseData();
		try {
			JSONArray jsonArray = companyService.getCompanyTree("0");
			List<Map<String,Object>> beanList = companyService.zzCompanList(jsonArray,-1);
			for (Map<String, Object> map : beanList) {
				String companyUUID = (String) map.get("uuid");
				List<Department> deptList = this.companyService.queryForBeanList(Department.class, "where companyUUID='"+companyUUID+"'");
			    if(!deptList.isEmpty()){
			    	map.put("deptList", deptList);
			    }
			}
			responseData.setCode("00");
			responseData.setData(beanList);
			responseData.setMsg("获取信息成功");
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setCode("01");
			responseData.setMsg("获取信息失败");
		}
		return responseData;
	}
	
	/**
	 * 得到全部[单位]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getAllCompanyList.do",method = RequestMethod.POST)
	public ResponseData getCompanyAllList() {
		ResponseData responseData = new ResponseData();
		try {
			JSONArray jsonArray = companyService.getCompanyTree("0");
			List<Map<String,Object>> beanList = companyService.zzCompanList(jsonArray,-1);
			responseData.setCode("00");
			responseData.setData(beanList);
			responseData.setMsg("获取信息成功");
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setCode("01");
			responseData.setMsg("获取信息失败");
		}
		return responseData;
	}
	
	/**
	 * 分页获取管理员列表
	 * @param adminForm
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getAdminList.do",method = RequestMethod.POST)
	public Map<String,Object> getAdminListByPage(AdminForm adminForm) {
		Map<String,Object> gridMap = new HashMap<String,Object>();
		PageView<AdminUser> pageView = new PageView<AdminUser>();
		try {
			pageView = this.adminUserService.getAdminListByPage(adminForm);
			gridMap.put("total", pageView.getTotalRecord());
			gridMap.put("rows", pageView.getRecords());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return gridMap;
	}
	
	/**
	 * 用户登录
	 * @param user 用户对象
	 * @param session
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/login.do",method = RequestMethod.POST)
	@AnoSysLog(actionName="后台登录系统")
	public ResponseData login(@RequestParam(required=true) String userName,@RequestParam(required=true) String password){
		LogUtils.info("username:"+userName+",password:"+password);
		ResponseData responseData = new ResponseData();
		try {
			//核实用户名在数据中是否存在
			boolean isExist = adminUserService.isExistUser(userName);
			if(isExist){
				AdminUser adminUser = adminUserService.login(userName,StringEncrypt.SHA256(password));
				if (null!=adminUser) {
					if(adminUser.getLocked()==1){
						responseData.buildResult(ResponseEnum.USER_IS_LOCKED);
						return responseData;
					}
					//改变登录次数
					this.adminUserService.updateLoginInfo(adminUser);
					adminUser.setLoginCount(adminUser.getLoginCount()+1);
					responseData.setData(adminUser);
					webUtil.setSessionAttr("adminUser", adminUser);
					Group group = this.adminUserService.getById(Group.class,adminUser.getGroupId());
					webUtil.setSessionAttr("group", group);
				} else {
					responseData.buildResult(ResponseEnum.PASSWORD_FAILD);
				}
			}else{
				responseData.buildResult(ResponseEnum.USER_NULL);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("logn.responseData:"+responseData.toString());
		return responseData;
	}
	
	/**
	 * 拿到当前登录的信息
	 * @param user 用户对象
	 * @param session
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getLoginInfo.do",method = RequestMethod.POST)
	@AnoSysLog(actionName="获取当前管理人员的登录信息")
	public ResponseData getLoginInfo(int id){
		ResponseData responseData = new ResponseData();
		try {
			AdminUser adminUser = this.adminUserService.getAdminUserById(id);
			if(null!=adminUser){
				responseData.setData(adminUser);
			}else{
				responseData.failue();
				return responseData;
			}
		}catch (Exception e) {
			LogUtils.info("getCurLoginInfo.err.登录异常：", e);
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
	
}
